package com.qs.rocketmq.bt.mq.selector;

import com.qs.rocketmq.bt.mq.MqConstant;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

/**
 * 消息消费端：消息设置属性发送，消费端根据属性进行sql过滤测试
 *
 * @since 2022-07-18 22:59:22
 */
public class MsgSendDemo {

    public static void main(String[] args) throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer(MqConstant.PRODUCER_GROUP_NAME);
        producer.setNamesrvAddr(MqConstant.MQ_NAME_SRV_ADDR);

        producer.start();

        String topic = MqConstant.TEST_TOPIC;
        String tag = "A";
        for (int i = 0; i < 10; i++) {
            Message msg = new Message(topic, tag, ("Hello RocketMQ " + i).getBytes(StandardCharsets.UTF_8));
            // 给每个消息设置 age 属性
            msg.putUserProperty("age", String.valueOf(i));
            SendResult sendResult = producer.send(msg);
            System.out.println(Thread.currentThread().getName() + "，" + sendResult);
        }

        producer.shutdown();
    }
}
